﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DapperExtensions;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.Orders;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.Order;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.Order
{
    public class OrderDiscountReponsitory : SqlServerRespository<DiscountResultEntity>, IOrderDiscountReponsitory
    {
        public OrderDiscountReponsitory()
        {
            Context = new WechatMallContext();
        }

        public DiscountResultDto GetDiscountResult(int discountTypeId, IList<DiscountGoodDetailEntity> goods)
        {
            if (goods != null)
            {
                var insertActions = new List<Action<IDbConnection>>
                {
                    ExecuteProcAddParams(goods, true),
                    ExecuteProcAddParams(new List<DiscountResultEntity>(), true),
                    ExecuteProcAddParams(new List<ProductDiscountResultEntity>(), true)
                };

                DiscountResultEntity discountResultEntity = null;
                var products = new List<ProductDiscountResultEntity>();
                ExecuteProc(insertActions, "pr_weixshangch_getcjje_fill",
                    new {zklbid = discountTypeId},
                    conn =>
                    {
                        discountResultEntity = conn
                            .GetList<DiscountResultEntity>(null, null, null, null, false)
                            .FirstOrDefault();
                        products = conn
                            .GetList<ProductDiscountResultEntity>(null, null, null, null, false).ToList();
                    });

                return new DiscountResultDto
                {
                    DiscountResultEntity = discountResultEntity,
                    ProductDiscountResult = products
                };
            }
            return new DiscountResultDto();
        }
    }
}